<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="common/commonLayUI :: common_head_LayUi(~{::title},~{},~{})">
    <meta charset="UTF-8">
    <title>标准目录</title>
</head>
<body>
<div>
    <div id="layer-open-Mulu">
        <form class="layui-form" action="" th:object="${dataInfo}">
            <div class="layui-form-item" style="display: none">
                <label class="layui-form-label">分类Id</label>
                <div class="layui-input-block">
                    <input type="text" name="id" id="id" placeholder="分类Id" th:value="*{id}"
                           autocomplete="off" class="layui-input layui-disabled" style="margin-right: 10px">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分类名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" required lay-verify="required" placeholder="分类名称"
                           th:value="*{title}"
                           autocomplete="off" class="layui-input" style="margin-right: 10px">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上级分类</label>
                <div class="layui-input-block">
                    <select name="parentId" id="parentId" lay-verify="required" th:field="*{parentId}">
                        <option value="">请选择上级分类</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <div class="layui-btn  layui-btn-primary" lay-filter="backUrl" onclick="closeLayer()">
                        返回
                    </div>
                    <div class="layui-btn layui-btn-normal" lay-filter="submitForm" lay-submit>确定</div>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['table', 'form', 'layer'], function () {
        // 渲染下拉框
        selectLoad();
        var form = layui.form;
        var layer = layui.layer;
        //监听提交
        form.on('submit(submitForm)', function (data) {
            let sendData = JSON.stringify(data.field);
            axios.post('/qa/biaozhun/muluUpdate', sendData, {
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(resp => {
                if (resp.data.code == 200) {
                    /*延迟回去*/
                    layer.msg(resp.data.msg, {icon: 1, time: 1000}, function () {
                        closeLayer();
                    })
                }
            }).catch(error => {
                console.log(error)
            });
            return true;
        });
    });

    function selectLoad() {
        var self_id = $("#id").val();
        axios.post('/qa/biaozhun/getMuluSelect', {}, {
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(resp => {
            if (resp.data.code == 200) {
                for (var i = 0; i < resp.data.data.length; i++) {
                    $("#parentId").append("<option value=\"" + resp.data.data[i].id + "\">" + resp.data.data[i].title + "</option>");
                }
                if (self_id != null && self_id != '' && self_id != undefined) {
                    $("#parentId").val([[${dataInfo.parentId}]]);
                }
                layui.form.render("select");
            }
        }).catch(error => {
            console.log(error)
        });
    }

    function closeLayer() {
        parent.location.reload();
        // 关闭自己
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    }
</script>
</body>
</html>
